package com.gitee.wsl.compose.ui.container.card

import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.animateValue
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.keyframes
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.hoverable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsHoveredAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.unit.IntSize
import com.gitee.wsl.compose.ui.base.Card


//https://juejin.cn/post/7300784595619233819
//这个url是我发现这个效果的网址
@Composable
fun Card.Flowing(
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit,
) {
    val shape = remember { RoundedCornerShape(8.dp) }
    var size by remember { mutableStateOf(IntSize.Zero) }
    val interactionSource = remember { MutableInteractionSource() }
    val hover by interactionSource.collectIsHoveredAsState()
    val infiniteTransition = rememberInfiniteTransition()
    val center by infiniteTransition.animateValue(
        Offset.Zero,
        Offset(size.width.toFloat(), size.height.toFloat()),
        typeConverter = Offset.VectorConverter,
        infiniteRepeatable(
            keyframes {
                durationMillis = 2000   //动画执行时长
                Offset.Zero at 0
                Offset(size.width.toFloat(), 0f) at 400
                Offset(size.width.toFloat(), size.height.toFloat()) at 1000
                Offset(0f, size.height.toFloat()) at 1400
                Offset.Zero at 2000
            },
            repeatMode = RepeatMode.Restart
        )
    )
    Box(
        modifier = Modifier
            .size(190.dp, 250.dp)
            .onGloballyPositioned {
                size = it.size
            }
            .background(Color.Black, shape)
            .hoverable(interactionSource)
            .border(
                4.dp, Brush.radialGradient(
                    listOf(
                        Color(1, 209, 125),
                        Color.Transparent
                    ),
                    center = center,
                    radius = 100f
                ), shape
            ),
        contentAlignment = Alignment.Center,
    ) {
        content()
    }
}
